# 智能家庭系统启动脚本

# 设置UTF-8编码
$OutputEncoding = [System.Text.Encoding]::UTF8

# 1. 启动MongoDB服务
Write-Host "1. 启动MongoDB服务..."
Start-Process -FilePath "cmd.exe" -ArgumentList "/k mongod --dbpath mongodb-data" -WindowStyle Normal -WorkingDirectory (Get-Location)

# 2. 等待MongoDB启动
Write-Host "2. 等待MongoDB启动..."
Start-Sleep -Seconds 5

# 3. 启动后端服务
Write-Host "3. 启动后端服务..."
Start-Process -FilePath "cmd.exe" -ArgumentList "/k cd backend && node index.js" -WindowStyle Normal -WorkingDirectory (Get-Location)

# 4. 等待后端服务启动
Write-Host "4. 等待后端服务启动..."
Start-Sleep -Seconds 5

# 5. 安装前端依赖（如果node_modules不存在）
if (-not (Test-Path -Path "frontend\node_modules")) {
    Write-Host "5. 正在安装前端依赖..."
    Start-Process -FilePath "cmd.exe" -ArgumentList "/k cd frontend && npm install" -WindowStyle Normal -WorkingDirectory (Get-Location)
    Write-Host "   等待依赖安装完成..."
    Start-Sleep -Seconds 30  # 根据网络情况调整时间
}

# 6. 启动前端服务
Write-Host "6. 启动前端服务..."
Start-Process -FilePath "cmd.exe" -ArgumentList "/k cd frontend && npm run dev" -WindowStyle Normal -WorkingDirectory (Get-Location)

# 7. 等待前端服务启动
Write-Host "7. 等待前端服务启动..."
Start-Sleep -Seconds 5

# 8. 自动打开浏览器
Write-Host "8. 打开浏览器..."
Start-Process -FilePath "http://localhost:5173"

# 9. 显示启动完成信息
Write-Host "
所有服务已启动！
"
Write-Host "前端地址: http://localhost:5173"
Write-Host "后端地址: http://localhost:3000"
Write-Host "
按任意键关闭此窗口..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')